﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Flight
{
    class Quad
    {
        public readonly VertexPositionNormalTexture[] Vertices;
        public readonly short[] Indexes;
        Vector3 Origin, Normal, Up, Left,
            UpperLeft, UpperRight, LowerLeft, LowerRight;
        public float leftTC, rightTC, topTC, bottomTC;

        private Quad(Vector3 center, Vector3 normal, Vector3 up, 
            float width, float height,
            float leftTC, float rightTC, float topTC, float bottomTC)
        {
            normal.Normalize();
            up.Normalize();

            Vertices = new VertexPositionNormalTexture[4];
            Indexes = new short[6];
            Origin = center;
            Normal = normal;
            Up = up;

            this.leftTC = leftTC;
            this.rightTC = rightTC;
            this.topTC = topTC;
            this.bottomTC = bottomTC;

            // Calculate the quad corners
            Left = Vector3.Cross(normal, Up);
            Vector3 uppercenter = (Up * height / 2) + center;
            UpperLeft = uppercenter + (Left * width / 2);
            UpperRight = uppercenter - (Left * width / 2);
            LowerLeft = UpperLeft - (Up * height);
            LowerRight = UpperRight - (Up * height);

            FillVertices();
            setTextureCoords();
        }
        public static Quad FromCenter(Vector3 center, Vector3 normal, Vector3 up, 
            float width, float height,
            float leftTC, float rightTC, float topTC, float bottomTC)
        {
            return new Quad(center, normal, up, width, height, leftTC, rightTC, topTC, bottomTC);
        }
        public static Quad FromCorner(Vector3 corner, Vector3 normal, Vector3 up,
            float width, float height,
            float leftTC, float rightTC, float topTC, float bottomTC)
        {
            normal.Normalize();
            up.Normalize();
            Vector3 right = Vector3.Cross(up, normal);
            return Quad.FromCenter(corner + right * width / 2 + up * height / 2, normal, up,
                width, height, leftTC, rightTC, topTC, bottomTC);
        }

        public void setTextureCoords()
        {
            Vertices[0].TextureCoordinate = new Vector2(leftTC, bottomTC);
            Vertices[1].TextureCoordinate = new Vector2(leftTC, topTC);
            Vertices[2].TextureCoordinate = new Vector2(rightTC, bottomTC);
            Vertices[3].TextureCoordinate = new Vector2(rightTC, topTC);
        }

        private void FillVertices()
        {
            // Provide a normal for each vertex
            for (int i = 0; i < Vertices.Length; i++)
            {
                Vertices[i].Normal = Normal;
            }

            // Set the position and for each vertex
            Vertices[0].Position = LowerLeft;
            Vertices[1].Position = UpperLeft;
            Vertices[2].Position = LowerRight;
            Vertices[3].Position = UpperRight;

            // Set the index buffer for each vertex, using clockwise winding
            Indexes[0] = 0;
            Indexes[1] = 1;
            Indexes[2] = 2;
            Indexes[3] = 2;
            Indexes[4] = 1;
            Indexes[5] = 3;
        }

    }
}
